#-*-coding:utf-8-*-
"""情感分析"""
import requests
import urllib3
urllib3.disable_warnings()
import json
import random
import time
import re
import json
import urllib.request
# from __future__ import print_function,unicode_literals
from aip import AipNlp
import jiagu



# TOKEN = GetToken().returnToken()

class Emotion(object):
    CONUT = 0
    @classmethod
    def post(cls, text, title=None):
        count = 0
        item = {
            "positive_prob": 0.5,
            "negative_prob": 0.5,
            "type": 0
        }
        while 1:
            try:
                sentiment = jiagu.sentiment(text)
                if sentiment[0] == 'positive': #正面
                    item['positive_prob'] = sentiment[1]
                    item['type'] = 1
                elif sentiment[0] == 'negative':
                    item['negative_prob'] = sentiment[1]
                    item['type'] = -1
                else:
                    print(sentiment)
                    item['type'] = 0
                break
            except Exception as e:
                print(e)
                time.sleep(random.randint(2, 3))
                if count > 20:
                    break
                count += 1

        print('######################分析结束＃＃＃＃＃＃＃＃＃＃＃',item)
        return item



#
# if __name__ == '__main__':
#     str = "很垃圾"
#     # str = str.replace('，','')
#     # print(str)
#     # print(type(str))
#     # str = Untiles().check_china(str)[:1000]
#     # print(len(list(str)))
#     # res = Emotion.post(str)
#     # res = Emotion.post(str,title='hah')
#     # print('jaj')
#     print(GetToken().returnToken())